import { Component, OnInit, Input } from '@angular/core';
// 导入表单相关：表单验证、表单构建、表单组
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

import { NzModalRef, NzMessageService, NzTabsModule } from 'ng-zorro-antd';
import { LicenseService } from 'src/app/core/service/license.service';

@Component({
  selector: 'app-license-modal',
  templateUrl: './license-modal.component.html',
  styleUrls: ['./license-modal.component.scss']
})
export class LicenseModalComponent implements OnInit {
  @Input() title: string;
  @Input() subtitle: string;
  // data = [];
  // licenseForm: FormGroup;
  // === new ===
  postData = {};
  licenseForm: FormGroup; // 许可
  // 自由软件基金会select
  selectFsfLibreVal = '';
  // 接受开放系统互联
  selectOsiApprovedVal = '';
  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private licenseService: LicenseService,
    fb: FormBuilder
  ) {
    // this.licenseForm = fb.group({
    //   reference: [null],
    //   isDeprecatedLicenseId: ['0'],
    //   detailsUrl: [null],
    //   referenceNumber: [null],
    //   name: [null, Validators.required],
    //   licenseId: [null, Validators.required],
    //   isOsiApproved: ['0'],
    // });

    this.licenseForm = fb.group({
      name: [null, Validators.required], // 许可名称
      licenseId: [null, Validators.required], // 许可id
      isFsfLibre: [null], // 自由软件基金会
      isOsiApproved: [null], // 接受开放系统互联
      reference: [null], // 参考文档地址
      detailsUrl: [null], // 参考明细地址
      seeAlso: [null], // 其他参考文档地址
      licenseText: [null], // 许可文本信息
      standardLicenseHeaderTemplate: [null], // 标准的许可头模板
      standardLicenseTemplate: [null],  // 标准的许可模板
      licenseComments: [null], // 许可描述
      standardLicenseHeader: [null] // 标准的许可头信息
    });
  }

  // addLicense(): void {
  //   console.log(this.licenseForm.controls);
  //   // tslint:disable-next-line:forin
  //   for (const c in this.licenseForm.controls) {
  //     this.licenseForm.controls[c].markAsTouched();
  //   }
  //   if (this.licenseForm.valid) {
  //     this.data = [{
  //       reference: null,
  //       isDeprecatedLicenseId: null,
  //       detailsUrl: null,
  //       referenceNumber: null,
  //       name: this.licenseForm.value.name,
  //       licenseId: this.licenseForm.value.licenseId,
  //       isOsiApproved: null,
  //     }];
  //     console.log(' ====this.data === ', this.data);
  //     this.licenseService.addLicense(this.data).subscribe((addResult) => {
  //       // this.result = result;   // 返回的结果
  //       console.log('==result==', addResult);
  //       if (addResult.status === 1 && addResult.msg === 'OK') {
  //         this.message.success('添加成功!', {
  //           nzDuration: 10000
  //         });
  //         this.modal.destroy({ data: 'this the result data' });
  //       } else {
  //         this.message.error('添加失败!请检查后重新填写', {
  //           nzDuration: 10000
  //         });
  //       }
  //     });
  //   } else {
  //     this.message.error('表单校验失败', { nzDuration: 10000 });
  //   }
  // }
  addLicense() {
    console.log(this.licenseForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.licenseForm.controls) {
      this.licenseForm.controls[c].markAsTouched();
    }

    if (this.licenseForm.valid) {
      this.postData = {
        name: this.licenseForm.value.name,
        licenseId: this.licenseForm.value.licenseId,
        fsfLibre: (this.licenseForm.value.isFsfLibre === 1) ? true : false,
        osiApproved: (this.licenseForm.value.isOsiApproved === 1) ?  true : false,
        reference: this.licenseForm.value.reference,
        detailsUrl: this.licenseForm.value.detailsUrl,
        seeAlso: this.fomatSeeAlso(this.licenseForm.value.seeAlso),
        licenseText: this.licenseForm.value.licenseText,
        standardLicenseHeaderTemplate: this.licenseForm.value.standardLicenseHeaderTemplate,
        standardLicenseTemplate: this.licenseForm.value.standardLicenseTemplate,
        licenseComments: this.licenseForm.value.licenseComments,
        stabdardLicenseHeader: this.licenseForm.value.standardLicenseHeader
      };
      console.log('=== this.postData ===', this.postData);
      this.addPostData(this.postData);
      // window.localStorage.removeItem('ifLibrarySecond');
      // window.localStorage.removeItem('secondLibraryForm');
    } else {
      this.message.error('添加失败!请检查后重新填写', {
        nzDuration: 10000
      });
    }
  }
  // post-data-part
  addPostData(postData) {
    let postArr = [];
    postArr.push(postData);
    console.log(' ====this.data === ', postArr);
    this.licenseService.addLicense(postArr).subscribe((addResult) => {
      // this.result = result;   // 返回的结果
      console.log('==result==', addResult);
      if (addResult.status === 1 && addResult.msg === 'OK') {
        this.message.success('添加成功!', {
          nzDuration: 10000
        });
        // window.localStorage.removeItem('ifLibrarySecond');
        // window.localStorage.removeItem('secondLibraryForm');
        this.modal.destroy({ data: 'this the result data' });
      } else {
        this.message.error('添加失败!请检查后重新填写', {
          nzDuration: 10000
        });
      }
    });
  }
  //  formatSeeAlso
  private fomatSeeAlso(str) {
    let arr = str.split(',');
    let targetArr = [];
    if (arr.length) {
      for (let i = 0; i < arr.length; i++) {
        let obj = { url: '' };
        obj.url = arr[i];
        targetArr.push(obj);
      }
      return targetArr;
    }
  }
  destroyModal(): void {
    // this.getSecondForm('set');
    this.modal.destroy({ data: 'this the result data' });
  }
  // 获取未提交的表单数据
  // getSecondForm(type) {
  //   if (type === 'set') { // 存储未提交的表单
  //     let data = {
  //       name: this.licenseForm.value.name ? this.licenseForm.value.name : null,
  //       licenseId: this.licenseForm.value.licenseId ? this.licenseForm.value.licenseId : null,
  //       isFsfLibre: this.licenseForm.value.isFsfLibre ? this.licenseForm.value.isFsfLibre : null,
  //       isOsiApproved: this.licenseForm.value.isOsiApproved ? this.licenseForm.value.isOsiApproved : null,
  //       reference: this.licenseForm.value.reference ? this.licenseForm.value.reference : null,
  //       detailsUrl: this.licenseForm.value.detailsUrl ? this.licenseForm.value.detailsUrl : null,
  //       seeAlso: this.licenseForm.value.seeAlso ? this.licenseForm.value.seeAlso : null,
  //       licenseText: this.licenseForm.value.licenseText ? this.licenseForm.value.licenseText : null,
  //       // tslint:disable-next-line: max-line-length
  //       standardLicenseHeaderTemplate: this.licenseForm.value.standardLicenseHeaderTemplate ? this.licenseForm.value.standardLicenseHeaderTemplate : null,
  //       standardLicenseTemplate: this.licenseForm.value.standardLicenseTemplate ? this.licenseForm.value.standardLicenseTemplate : null,
  //       licenseComments: this.licenseForm.value.licenseComments ? this.licenseForm.value.licenseComments : null,
  //       standardLicenseHeader: this.licenseForm.value.standardLicenseHeader ? this.licenseForm.value.standardLicenseHeader : null,
  //     };
  //     window.localStorage.setItem('secondLicenseForm', JSON.stringify(data));
  //     window.localStorage.setItem('ifLicenseSecond', '1');
  //     console.log('=-=', this.licenseForm.value);
  //   } else if (type === 'get') { // 获取上一次未提交的表单
  //     let jsonObj = JSON.parse(window.localStorage.getItem('secondLicenseForm'));
  //     this.selectFsfLibreVal = jsonObj.isFsfLibre;
  //     this.selectOsiApprovedVal = jsonObj.isOsiApproved;

  //     this.licenseForm.patchValue({
  //       name: jsonObj.name,
  //       licenseId: jsonObj.licenseId,
  //       reference: jsonObj.reference,
  //       detailsUrl: jsonObj.detailsUrl,
  //       seeAlso: jsonObj.seeAlso,
  //       licenseText: jsonObj.licenseText,
  //       // tslint:disable-next-line: max-line-length
  //       standardLicenseHeaderTemplate: jsonObj.standardLicenseHeaderTemplate,
  //       standardLicenseTemplate: jsonObj.standardLicenseTemplate,
  //       licenseComments: jsonObj.licenseComments,
  //       standardLicenseHeader: jsonObj.standardLicenseHeader
  //     });
  //     console.log('-=-', this.licenseForm.value);
  //   }
  // }
  ngOnInit() {
    // get-formData
    // if (window.localStorage.getItem('ifLicenseSecond') === '1') {
    //   this.getSecondForm('get');
    // }
  }

}
